/*
 * Copyright 2016-2019 yoara
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package yhao.infra.service.data.entitydiff;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * data-entityDiff数据对比传参数据结构类
 *
 * @author yoara
 */
public class DiffParam extends BaseDiffParam{
    //对象类型
    private Class entityClass;
    //包含对象
    private List<String> includeField;
    //不包含对象
    private List<String> excludeField;

    public List<String> getIncludeField() {
        return includeField;
    }

    public void setIncludeField(List<String> includeField) {
        this.includeField = includeField;
    }

    public List<String> getExcludeField() {
        return excludeField;
    }

    public void setExcludeField(List<String> excludeField) {
        this.excludeField = excludeField;
    }

    public Class getEntityClass() {
        return entityClass;
    }

    public void setEntityClass(Class entityClass) {
        this.entityClass = entityClass;
    }

    /**
     * 获取欲对比的字段
     * {@link DiffParam#excludeField}，{@link DiffParam#includeField}
     */
    public List<Field> pickDiffFieldList(){
        Field[] fields = historyEntity.getClass().getDeclaredFields();
        List<Field> list = new ArrayList<>();
        for(Field field : fields){
            //如果参数include不为空，则只判断这些字段
            if(includeField!=null){
                if(includeField.contains(field.getName())){
                    field.setAccessible(true);
                    list.add(field);
                }
            }else{
                //如果参数exclude不为空，则排除这些字段
                if(excludeField!=null){
                    if(!excludeField.contains(field.getName())){
                        field.setAccessible(true);
                        list.add(field);
                    }
                }
            }
        }
        return list;
    }

    /**
     * 获取待对比的字段关联的Field
     * {@link DiffParam#pickDiffFieldList()}
     */
    public Map<String, Field> pickEntityFieldMap(List<Field> diffFieldList) {
        Field[] fields = entity.getClass().getDeclaredFields();
        Map<String, Field> map = new HashMap<>();
        for(Field field:diffFieldList){
            for(Field fh: fields){
                if(field.getName().equals(fh.getName())){
                    fh.setAccessible(true);
                    map.put(fh.getName(),fh);
                    break;
                }
            }
        }
        return map;
    }
}
